/**
 * 
 */
package org.vt.ece3574.pictoslider.puzzle;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * Touch Event Handler for the PuzzlePiece class
 * 
 * @author Michael Drescher
 * 
 */
public class PuzzleTouchHandler implements OnTouchListener {

	private float touchStartLocationX;
	private float touchStartLocationY;
	private float touchEndLocationX;
	private float touchEndLocationY;

//	private boolean actionStarted;
	private boolean actionEnded;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 * android.view.MotionEvent)
	 */
	public boolean onTouch(View v, MotionEvent event) {

		PuzzlePiece piece = (PuzzlePiece) v;
		Puzzle puzzle = piece.getPuzzle();

		// If the piece can be moved, determine the motion event specifics.
		if (piece.isMoveable()) {

			// Get event information
			int action = event.getActionMasked();
			switch (action) {

			// On the first touch, get the X and Y positions of the touch.
			case MotionEvent.ACTION_DOWN:
				touchStartLocationX = event.getX();
				touchStartLocationY = event.getY();
//				actionStarted = true;
				actionEnded = false;
				break;

			// On the last touch, store the X and Y positions.
			case MotionEvent.ACTION_UP:
				touchEndLocationX = event.getX();
				touchEndLocationY = event.getY();
				actionEnded = true;
//				actionStarted = false;
				break;
			}

			// Get a reference to the blank piece, as well as the position
			// of the blank piece relative to the touched piece.
			PuzzlePiece[] neighbors = puzzle.getNeighborsForPiece(piece);
			PuzzlePiece blankPiece = null;
			int blankPosition = -1;
			for (int i = 0; i < neighbors.length; i++) {
				if (neighbors[i] != null) {
					if (neighbors[i].isBlankPiece()) {
						blankPiece = neighbors[i];
						blankPosition = i;
					}
				}
			}

			// Get the change in X and Y coordinates
//			float deltaX = 0, deltaY = 0;
//			if (actionStarted) {
//				deltaX = touchStartLocationX - event.getX();
//				deltaY = touchStartLocationY - event.getY();
//
//				// Will change to true if the piece needs to be moved as
//				// a result of this event (i.e. piece is moved towards the blank
//				// piece).
//				boolean animate = ((deltaX < 0) && (blankPosition == 0))
//						|| ((deltaX > 0) && (blankPosition == 1))
//						|| ((deltaY < 0) && (blankPosition == 2))
//						|| ((deltaY > 0) && (blankPosition == 3));
//
//				// If the piece must be animated, animate it in the correct
//				// direction.
//				if (animate) {
//					if (blankPosition < 2)
//						piece.setTranslationX(deltaX);
//					else
//						piece.setTranslationY(deltaY);
//				}
//			}
			if (actionEnded) {

				boolean swap = false; // Will change to true if pieces must be
										// swapped.

				// If the total motion is in the correct direction, and is great
				// enough to warrant a swap of the pieces, then set swap to
				// true.
				switch (blankPosition) {
				case 0:
					swap = (touchEndLocationX - touchStartLocationX) < -1
							* (piece.getWidth() / 2);
					break;
				case 1:
					swap = (touchEndLocationX - touchStartLocationX) > (piece
							.getWidth() / 2);
					break;
				case 2:
					swap = (touchEndLocationY - touchStartLocationY) < -1
							* (piece.getWidth() / 2);
					break;
				case 3:
					swap = (touchEndLocationY - touchStartLocationY) > (piece
							.getWidth() / 2);
					break;
				}

				// Make all pieces around the blank one unmoveable.
				PuzzlePiece[] blankNeighbors = puzzle
						.getNeighborsForPiece(blankPiece);
				for (int i = 0; i < blankNeighbors.length; i++) {
					if (blankNeighbors[i] != null) {
						blankNeighbors[i].setMoveable(false);
					}
				}

				// Update the puzzle with the new location of the pieces if
				// necessary.
				if (swap)
					puzzle.swap(piece, blankPiece);

//				piece.setTranslationX(0);
//				piece.setTranslationY(0);

				// Update which pieces are moveable
				if(!(puzzle.isSolved())){
					blankNeighbors = puzzle.getNeighborsForPiece(blankPiece);
					for (int i = 0; i < blankNeighbors.length; i++) {
						if (blankNeighbors[i] != null) {
							blankNeighbors[i].setMoveable(true);
						}
					}
				}
			}
		}
		return true;
	}

}
